package com.yds.modewithprinceple.prototype;

/**
 * @author YDS
 * @date 2021/5/7
 * @discribe 深拷贝的只读保护
 */
public class Master {
    private Master() {
    }

    private static Master master;
    private Person person;

    public static synchronized Master getInstance() {
        if (master == null) {
            master = new Master();
        }

        return master;
    }

    public Person getPerson() {
//        return person;
        return (Person) person.clone();
    }

    /**
     * 只能通过包级私方法去设置修改
     *
     * @param person
     */
    protected void setPerson(Person person) {
        this.person = person;
    }

    /**
     * 模拟同包下设置person属性
     */
    public void setOriginalPerson() {
        person = new Person();
        person.setName("zhangsan");
        person.setAge(1);
    }

    /**
     * 模拟同包下获取person属性
     */
    public Person getOriginalPerson() {
        return person;
    }

}
